<template>
  <div class="app-container">

    <!-- 相当于一个大的div 里面包含了两个模块，左边和右边 -->
  <el-row :gutter="20">


    <!-- 左侧区域树形接口 -->
      <el-col :span="4" :xs="24">
          <div class="head-container">
            <el-input
              v-model="areaName"
              placeholder="请输入组织名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="areaOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              default-expand-all
              @node-click="handleNodeClick"
            />
          </div>
        </el-col>
            <!-- 左侧区域树形接口     -->

     <!-- 注释 -->
    <!-- 右侧厂站数据 -->
    <el-col :span="20" :xs="24">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="厂站名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入厂站名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="厂站类型" prop="stationtype">
        <el-select v-model="queryParams.stationtype" placeholder="请选择站点类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>


      <el-form-item label="负责人" prop="contactperson">
        <el-input
          v-model="queryParams.contactperson"
          placeholder="请输入负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

   
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:vstation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:vstation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:vstation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:vstation:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="vstationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="厂站标识" align="center" prop="stationcode" />
      <el-table-column label="厂站名称" align="center" prop="name" />
      <el-table-column label="厂站地址" align="center" prop="address" />
      <el-table-column label="区域id" align="center" prop="areaId" />
      <el-table-column label="负责人" align="center" prop="contactperson" />
      <el-table-column label="负责人电话" align="center" prop="contactphone" />
      <el-table-column label="帐号状态" align="center" prop="state" />
      <el-table-column label="服务户数" align="center" prop="servicepeople" />
      <el-table-column label="处理能力" align="center" prop="processcapacity" />
      <el-table-column label="建厂时间" align="center" prop="builddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.builddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="厂站类型" align="center" prop="stationtype" />
      <el-table-column label="经度" align="center" prop="lng" />
      <el-table-column label="采集频率" align="center" prop="readcycle" />
      <el-table-column label="设备唯一标识" align="center" prop="sncode" />
      <!-- <el-table-column label="备用字段" align="center" prop="beiyong" /> -->
      <el-table-column label="纬度" align="center" prop="lat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:vstation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:vstation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-col>
    <!-- 右侧厂站数据 -->

    </el-row>
    <!-- 相当于一个大的div 里面包含了两个模块，左边和右边 -->



    <!-- 添加或修改厂站信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="厂站名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入厂站名称" />
        </el-form-item>
        <el-form-item label="厂站地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入厂站地址" />
        </el-form-item>
        <el-form-item label="区域id" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入区域id" />
        </el-form-item>
        <el-form-item label="负责人" prop="contactperson">
          <el-input v-model="form.contactperson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="contactphone">
          <el-input v-model="form.contactphone" placeholder="请输入负责人电话" />
        </el-form-item>
        <el-form-item label="帐号状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入帐号状态" />
        </el-form-item>
        <el-form-item label="服务户数" prop="servicepeople">
          <el-input v-model="form.servicepeople" placeholder="请输入服务户数" />
        </el-form-item>
        <el-form-item label="处理能力" prop="processcapacity">
          <el-input v-model="form.processcapacity" placeholder="请输入处理能力" />
        </el-form-item>
        <el-form-item label="建厂时间" prop="builddate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.builddate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择建厂时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="厂站类型">
          <el-select v-model="form.stationtype" placeholder="请选择厂站类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="采集频率" prop="readcycle">
          <el-input v-model="form.readcycle" placeholder="请输入采集频率" />
        </el-form-item>
        <el-form-item label="设备唯一标识" prop="sncode">
          <el-input v-model="form.sncode" placeholder="请输入设备唯一标识" />
        </el-form-item>
        <!-- <el-form-item label="备用字段" prop="beiyong">
          <el-input v-model="form.beiyong" placeholder="请输入备用字段" />
        </el-form-item> -->
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVstation, getVstation, delVstation, addVstation, updateVstation, exportVstation } from "@/api/configureSystem/vstation";
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/configureSystem/area";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Vstation",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 厂站信息表格数据
      vstationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
       // 厂站树选项
       areaOptions: undefined,
        // 厂站名称
      areaName: undefined,
       defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        address: undefined,
        areaId: undefined,
        contactperson: undefined,
        contactphone: undefined,
        state: undefined,
        servicepeople: undefined,
        processcapacity: undefined,
        builddate: undefined,
        stationtype: undefined,
        lng: undefined,
        readcycle: undefined,
        sncode: undefined,
        beiyong: undefined,
        lat: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        address: [
          { required: true, message: "站点地址不能为空", trigger: "blur" }
        ],
        areaId: [
          { required: true, message: "区域id不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "区域id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    // 根据名称筛选厂站树
    areaName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();

  },
 
  methods: {
    /** 查询厂站信息列表 */
    getList() {
      this.loading = true;
      listVstation(this.queryParams).then(response => {
        this.vstationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
      /** 查询厂站下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
        this.areaOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      // alert(22);点击左侧树形结构触发事件
      alert(data.id);//树形结构节点id
      this.queryParams.areaId = data.id;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stationcode: undefined,
        name: undefined,
        address: undefined,
        areaId: undefined,
        contactperson: undefined,
        contactphone: undefined,
        state: undefined,
        servicepeople: undefined,
        processcapacity: undefined,
        builddate: undefined,
        stationtype: undefined,
        lng: undefined,
        readcycle: undefined,
        sncode: undefined,
        beiyong: undefined,
        lat: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加厂站信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVstation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改厂站信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateVstation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addVstation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除厂站信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVstation(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有厂站信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportVstation(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>